package com.lkd.handler;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

@Component
@Slf4j
@Topic(TopicConfig.VMS_STATUS_TOPIC)
public class VmStatusHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;

    /**
     * 设备维修
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        if (StringUtils.isBlank(jsonMsg)){
            return;
        }
        VmStatusContract vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        Optional.ofNullable(vmStatusContract).ifPresent(vmStatus -> {
            //只要有一个部件故障,就创建维修工单
            List<StatusInfo> statusInfos = vmStatus.getStatusInfo();
            for (StatusInfo statusInfo : statusInfos) {
                if (!statusInfo.isStatus()){
                    taskService.AutoDistributeTask(vmStatus.getInnerCode(), VMSystem.TASK_TYPE_REPAIR,null);
                }
            }
        });
    }

}

















